package main.Primavera;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ConfigServerList extends JDialog{

    private JButton addButton;
    private JButton editButton;
    private JButton deleteButton;
    public JList list_server;
    private JScrollPane jScrollPane;
    private JPanel panel;
    public static DefaultListModel listModel;
    public DefaultComboBoxModel serverBoxModel;
    private ConfigXML configXML;

    private Dimension screensize;

    public void openServerList(DefaultComboBoxModel serverModel) {
        setModal(true);
        setTitle("Список серверов Prim");
        panel = new JPanel();
        setSize(new Dimension(280,180));
        screensize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screensize.width / 2 - 150, screensize.height / 2 - 150);

        GridBagConstraints c;
        panel.setLayout(new GridBagLayout());

        configXML = new ConfigXML();
        listModel = configXML.serverListJList();
        list_server = new JList(listModel);

        serverBoxModel = serverModel;

        jScrollPane = new JScrollPane(list_server);
        jScrollPane.setPreferredSize(new Dimension(300,200));

        addButton = new JButton("Добавить");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                PMConfig pmConfig = new PMConfig(serverBoxModel, listModel, list_server);
                pmConfig.Openconfig("", 0);
            }
        });

        editButton = new JButton("Редактировать");
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                int choose_server = list_server.getSelectedIndex();
                String name_choose_server = (String) listModel.get(choose_server);
                PMConfig pmConfig = new PMConfig(serverBoxModel, listModel, list_server);
                pmConfig.Openconfig(name_choose_server, choose_server);

            }
        });

        deleteButton = new JButton("Удалить");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                int choose_server = list_server.getSelectedIndex();
                String name_choose_server = (String) listModel.get(choose_server);
                listModel.remove(choose_server);
                serverBoxModel.removeElementAt(choose_server);
                configXML.deleteConfig(name_choose_server);
            }
        });

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 3;
        c.weightx = 1;
        c.weighty = 1;
        c.insets = new Insets(5,5,5,5);
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.NORTHWEST;
        panel.add(jScrollPane, c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(5,0,0,5);
        panel.add(addButton, c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(5,0,0,5);
        panel.add(editButton, c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.insets = new Insets(5,0,0,5);
        panel.add(deleteButton, c);

        add(panel);
        setVisible(true);

    }
}
